package cn.est.service.impl;

import cn.est.constants.Constants;
import cn.est.pojo.Brand;
import cn.est.pojo.BrandClassifyRelation;
import cn.est.pojo.Classify;
import cn.est.pojo.Model;
import cn.est.service.*;
import cn.est.util.RedisUtil;
import cn.est.vo.BrandVO;
import cn.est.vo.ClassifyVO;
import cn.est.vo.ModelVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class HomeServiceImpl implements HomeService {
    @Autowired
    private BrandClassifyRelationService brandClassifyRelationService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private ModelService modelService;
    @Autowired
    private RedisUtil redisUtil;


    @Override
    public List<ClassifyVO> getClassifyTree() {
        //从redis缓存中查询
        List<ClassifyVO> classifyVOList = null;
        try {
            classifyVOList = (List<ClassifyVO>) redisUtil.get(Constants.CLASSIFY_TREE);
            if (classifyVOList != null) {
                return classifyVOList;
            }
        }catch (Exception e) {
            log.error("从缓存中获取数据出错!",e);
        }

        //先查询类别
        List<Classify> classifyList = classifyService.listUpClassify();
        classifyVOList = new ArrayList<>();
        //根据类别查询品牌
        for (Classify classify : classifyList) {
            //把classify 对象转为classifyVO 对象
            ClassifyVO classifyVO = new ClassifyVO();
            BeanUtils.copyProperties(classify,classifyVO);
            //查询品牌Id
            Long classifyId = classify.getId();
            List<BrandVO> brandVOList = getBrandListByClassifyId(classifyId);
            classifyVO.setBrandList(brandVOList);
            classifyVOList.add(classifyVO);
        }
        //把数据保存到redis中
        try {
            redisUtil.setHours(Constants.CLASSIFY_TREE,classifyVOList,1);
        }catch (Exception e) {
            log.error("存数据到缓存中出错!",e);
        }

        return classifyVOList;
    }

    private List<BrandVO> getBrandListByClassifyId(Long classifyId) {
        List<BrandClassifyRelation> brandClassifyRelationList = brandClassifyRelationService.listBrandClassifyRelationByClassifyId(classifyId);
        //根据品牌Id查询品牌对象
        List<Brand> brandList = brandService.listUpBrandByIds(brandClassifyRelationList);
        List<BrandVO> brandVOList = new ArrayList<>();
        for (Brand brand : brandList) {
            //brand 对象转为 BrandVO 对象
            BrandVO brandVO = new BrandVO();
            BeanUtils.copyProperties(brand,brandVO);
            Long brandId = brand.getId();
            //根据品牌和类型查询型号
            List<ModelVO> modelVOList = getModelListByClassifyIdAndBrandId(classifyId, brandId);
            brandVO.setModelList(modelVOList);
            brandVOList.add(brandVO);
        }
        return brandVOList;
    }

    private List<ModelVO> getModelListByClassifyIdAndBrandId(Long classifyId,Long brandId){
        List<Model> modelList = modelService.listUpModelByClassifyIdAndBrandId(classifyId, brandId);
        List<ModelVO> modelVOList = new ArrayList<>();
        for (Model model : modelList) {
            ModelVO modelVO = new ModelVO();
            BeanUtils.copyProperties(model,modelVO);
            modelVOList.add(modelVO);
        }
        return modelVOList;
    }
}
